package com.rpframework.website.device.act.api;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.rpframework.core.utils.TagUtils;
import com.rpframework.utils.Pager;
import com.rpframework.website.device.act.admin.AdminAct;
import com.rpframework.website.device.domain.User;
import com.rpframework.website.device.domain.Widens;
import com.rpframework.website.device.exception.APICodeException;
import com.rpframework.website.device.service.WidensService;
/**
 * 公告
 * @author Andy_hqr
 * @date 2016年3月8日 下午7:45:28
 */

@Controller
@RequestMapping("api/widen")
public class ApiWidensAvt extends AdminAct{
	Gson gson=new Gson();
	@Resource WidensService widensService;
	
	
	
	//首页显示公告
	/**
	 * 当前登录用户所属小区的公告列表 
	 * @param pager
	 * @param model
	 * @param callback
	 * @param session
	 * @return
	 * @time 2016年3月8日 下午7:45:58
	 */
	@RequestMapping(value="/list",produces = "application/json; charset=utf-8")
	public @ResponseBody String list(@RequestParam(value="pager", required=false)Pager<Widens> pager, Map<Object, Object> model,
			@RequestParam(value = "callback",required = false ) String callback,
			HttpSession session){
		if(pager==null){
			pager=new Pager<Widens>();
		}
		User sessionuser=getSessionUser(session);
		if(sessionuser==null){
			throw new APICodeException(-4, "你还没登陆!");
		}
		//pager.getSearchMap().put("propertyid", String.valueOf(sessionuser.getPropertyid()));
		//当前登录用户所属小区的公告列表
		pager.getSearchMap().put("typeid", String.valueOf(2));
		pager=widensService.widenpager(pager);
		List<Widens> list=pager.getItemList();
		JsonObject json=new JsonObject();
		json.addProperty("totalPages", pager.getTotalPages());
		json.addProperty("currentPage", pager.getCurrentPage());
		json.addProperty("totalCount", pager.getTotalCount());
		JsonArray array=new JsonArray();
		json.add("arrays", array);
		for (Widens act : list) {
			JsonObject jsonObj = gson.toJsonTree(act).getAsJsonObject();
			jsonObj.addProperty("sytime", TagUtils.formatDate(act.getSytime()));
			array.add(jsonObj);
		}
		return (callback + "(" + json + ")");
	}
	
	//后台系统公告
	@RequestMapping(value="/listwineds",produces = "application/json; charset=utf-8")
	public @ResponseBody String listwineds(@RequestParam(value="pager", required=false)Pager<Widens> pager, Map<Object, Object> model,
			@RequestParam(value = "callback",required = false ) String callback,
			HttpSession session){
		if(pager==null){
			pager=new Pager<Widens>();
		}
		User sessionuser=getSessionUser(session);
		if(sessionuser==null){
			throw new APICodeException(-4, "你还没登陆!");
		}
		pager.getSearchMap().put("typeid", String.valueOf(1));
		pager=widensService.widenpager(pager);
		List<Widens> list=pager.getItemList();
		JsonObject json=new JsonObject();
		json.addProperty("totalPages", pager.getTotalPages());
		json.addProperty("currentPage", pager.getCurrentPage());
		json.addProperty("totalCount", pager.getTotalCount());
		JsonArray array=new JsonArray();
		json.add("arrays", array);
		for (Widens act : list) {
		
			JsonObject jsonObj = gson.toJsonTree(act).getAsJsonObject();
			jsonObj.addProperty("sytime", TagUtils.formatDate(act.getSytime()));
			array.add(jsonObj);
		}
		return (callback + "(" + json + ")");
	}
	
		/**
		 * 根据公告id查询公告详情
		 * @param windesid
		 * @param callback
		 * @param model
		 * @param session
		 * @return
		 * @time 2016年3月8日 下午8:13:54
		 */
		@RequestMapping(value="/listwinedstole",produces = "application/json; charset=utf-8")
		public @ResponseBody String listwineds(@RequestParam String windesid,
			@RequestParam(value = "callback",required = false ) String callback,
				Map<Object, Object> model,
				HttpSession session){
			Widens wind=widensService.selectOnlyOne(Integer.parseInt(windesid));
			JsonObject json=new JsonObject();
			json.addProperty("sytitle", wind.getSyTitle());
			json.addProperty("sycontent", wind.getSycontent());
			json.addProperty("syimages", wind.getSyimages());
			json.addProperty("sytime", TagUtils.formatDate(wind.getSytime()));
			json.addProperty("username", wind.getUsername());
			return (callback + "(" + json + ")");
		}
		
	
	
}
